# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gcc
_mingw_suff=mingw-w64-cross
pkgname=("${_mingw_suff}-${_realname}")
pkgver=4.9.2
pkgrel=3
pkgdesc="Cross GCC for the MinGW-w64"
arch=('i686' 'x86_64')
url="http://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
groups=("${_mingw_suff}-toolchain" "${_mingw_suff}")
depends=("zlib" "mpc" "cloog"
         "${_mingw_suff}-binutils"
         "${_mingw_suff}-crt"
         "${_mingw_suff}-headers"
         "${_mingw_suff}-winpthreads"
         "${_mingw_suff}-windows-default-manifest")
makedepends=("gcc" 'lndir' "gmp-devel" "mpc-devel" "cloog-devel" "zlib-devel" "isl-devel")
#checkdepends=('dejagnu')
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=("ftp://gcc.gnu.org/pub/gcc/releases/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.bz2"
        001-4.7-ada.patch
        005-4.7-boehm-gc-cygwin.patch
        010-4.7-execstack.patch
        015-4.7-java-awt.patch
        016-4.5-java-FIONREAD.patch
        017-4.7-java-jni.patch
        018-4.8-java-soname.patch
        025-4.8-libatomic-cygwin.patch
        030-4.7-libffi-noinst.patch
        031-4.8-libffi-cygwin64.patch
        035-4.7-libitm-cygwin.patch
        040-4.8-lto-plugin-soname.patch
        045-4.8-specs-cygwin.patch
        046-4.8-__cxa_atexit.patch
        047-4.8-libstdc-atexit_thread.patch
        048-4.8-medium-model-dllimport.patch
        050-4.5-skiptest.patch
        055-4.8-gcc-ada-DTR.patch
        060-4.8-Wimplicit-decls.patch
        065-4.9-prettify-linking-no-undefined.mingw.patch
        070-4.8-filename-output.patch
        075-4.8-pr-57440.patch
        080-4.8-libstdc++export.patch
        950-4.8.0-configure-msys2.patch
        951-4.8.0-msys2-spec.patch
        952-4.8.0-libgcc-msys2.patch
        953-4.9.1-testsuite-msys2.patch
        955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
        1000-gcc49-isl-0.14-hack.patch)
md5sums=('4df8ee253b7f3863ad0b86359cd39c43'
         '0b96824eeebc3d444ce5447ec84fe482'
         '403c251e9794c9d183a6be03254c7bd5'
         '7eb73a1fec83520e9015553e03e48198'
         '1a0d50cfe02607d433d96baf3abb44e6'
         'f0b31458215043dcae74179cccab334c'
         '9a73b38c60bdc1341a1cf436194fb20f'
         'ad99cd62e7a91e334d238e14d8cc7267'
         'ed4bda44f75a1f0c69179724162ee74b'
         'a2888ff671f3d1c91e29dbc0382ab64a'
         'd9a6aa18c94f805ecf97f876eac95222'
         'ac437553f3d705955d5a51a1689803ab'
         'a3aae6965efe954e0eb07aab83765bbc'
         '68a1a30297f6c45f785adc741d954469'
         'c691e17f3ae72dfc559811b3e0036332'
         '59c007fedfc7bd6e8541b7ad339addfd'
         '66fb25ef1fd7f0412bdd7ad23d921060'
         '2f12113913da4fc3a992f5611009a506'
         'aed26d95b8a03d502b74811f88fff8bb'
         '577735db62e410b221a16f00d3010f4d'
         '09bdad41515c8d353612f30c91b3bac2'
         'b05b64cd00dda6b14d22a98c68f7eefe'
         '347336d5a318e5e8b3cde29e9174413c'
         'f6b5fb08ff06a7d60ee3280e14240bb2'
         '82787563693b72ef96907179d11ae574'
         'e092603bca2a80d1df88800f3393ed16'
         '4092fcf2e81fbd292998633bef448cbe'
         'f2c3dc9f45f07d9da42289d117fed678'
         '983b08347388dfe506a09b8a317274fe'
         '944b730b936c2f44187e0638371d8e5f')

_threads="win32"
_targets="i686-w64-mingw32 x86_64-w64-mingw32"

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  #do not install libiberty
  sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  #sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  #Cygwin patches

  patch -p1 -i ${srcdir}/001-4.7-ada.patch
  patch -p1 -i ${srcdir}/005-4.7-boehm-gc-cygwin.patch
  patch -p1 -i ${srcdir}/010-4.7-execstack.patch
  patch -p1 -i ${srcdir}/015-4.7-java-awt.patch
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35536
  patch -p1 -i ${srcdir}/016-4.5-java-FIONREAD.patch
  patch -p1 -i ${srcdir}/017-4.7-java-jni.patch
  patch -p1 -i ${srcdir}/018-4.8-java-soname.patch
  patch -p1 -i ${srcdir}/025-4.8-libatomic-cygwin.patch
  patch -p1 -i ${srcdir}/030-4.7-libffi-noinst.patch
  patch -p1 -i ${srcdir}/031-4.8-libffi-cygwin64.patch
  patch -p1 -i ${srcdir}/035-4.7-libitm-cygwin.patch
  patch -p1 -i ${srcdir}/040-4.8-lto-plugin-soname.patch
  patch -p1 -i ${srcdir}/045-4.8-specs-cygwin.patch
  patch -p1 -i ${srcdir}/046-4.8-__cxa_atexit.patch
  patch -p1 -i ${srcdir}/047-4.8-libstdc-atexit_thread.patch
  #patch -p1 -i ${srcdir}/048-4.8-medium-model-dllimport.patch
  patch -p1 -i ${srcdir}/050-4.5-skiptest.patch

  if [ "$CARCH" = "i686" ]
  then
    patch -p1 -i ${srcdir}/055-4.8-gcc-ada-DTR.patch
  fi

  patch -p1 -i ${srcdir}/060-4.8-Wimplicit-decls.patch

  # Misc patches
  patch -p1 -i ${srcdir}/065-4.9-prettify-linking-no-undefined.mingw.patch
  patch -p1 -i ${srcdir}/070-4.8-filename-output.patch
  patch -p1 -i ${srcdir}/075-4.8-pr-57440.patch
  patch -p1 -i ${srcdir}/080-4.8-libstdc++export.patch

  #MSYS2 Patches
  patch -p1 -i ${srcdir}/950-4.8.0-configure-msys2.patch
  patch -p1 -i ${srcdir}/951-4.8.0-msys2-spec.patch
  patch -p1 -i ${srcdir}/952-4.8.0-libgcc-msys2.patch
  patch -p1 -i ${srcdir}/953-4.9.1-testsuite-msys2.patch
  patch -p1 -i ${srcdir}/955-4.9.2-apply-hack-so-gcc_s-isnt-stripped.patch
  
  # Compatibility with ISL-0.14.x
  patch -p0 -i ${srcdir}/1000-gcc49-isl-0.14-hack.patch
}

build() {
  for _target in ${_targets}; do
  mkdir -p ${srcdir}/gcc-build-${_target} && cd ${srcdir}/gcc-build-${_target}

  case "${_target}" in
    i686*)
      local _conf="--disable-sjlj-exceptions --with-dwarf2"
      local _arch=i686
    ;;

    x86_64*)
      local _arch=x86-64
      local _conf=""
    ;;
  esac

  ../${_realname}-${pkgver}/configure \
    --prefix=/opt \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${_target} \
    --libexecdir=/opt/lib \
    --disable-bootstrap \
    --with-arch=${_arch} \
    --with-tune=generic \
    --enable-languages=c,lto,c++,fortran \
    --enable-shared --enable-static \
    --enable-threads=${_threads} \
    --enable-graphite \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-time=yes \
    --disable-libstdcxx-pch \
    --disable-libstdcxx-debug \
    --enable-cloog-backend=isl \
    --enable-version-specific-runtime-libs \
    --disable-cloog-version-check --disable-isl-version-check \
    --enable-lto \
    --enable-libgomp \
    --disable-multilib \
    --enable-checking=release \
    --disable-rpath \
    --disable-win32-registry \
    --disable-werror \
    --disable-symvers \
    --with-libiconv \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl,cloog}=/usr \
    --with-gnu-as --with-gnu-ld \
    ${_conf} \
    CFLAGS="-O2 -pipe" \
    CXXFALGS="-O2 -pipe"

  make all
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/gcc-build-${_target}
    make -j1 DESTDIR=${pkgdir} install
    mv ${pkgdir}/opt/lib/gcc/${_target}/lib/libgcc_s.a ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    mv ${pkgdir}/opt/lib/gcc/${_target}/libgcc*.dll ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    for _f in ${pkgdir}/opt/bin/*.exe; do
      strip -s ${_f}
    done
    for _f in ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/*.exe; do
      strip -s ${_f}
    done
    rm -rf ${pkgdir}/opt/share
  done
}
